/**
 * Captain
 * © 2017 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 *
 * JAVA : 8
 * 文  件  名: DataDictionaryController.java
 * 创  建  人: sunfeng (sun.feng@sccaptain.com.cn)
 * 创建时间: 2017年7月4日 下午3:17:44
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.web.sysconfig.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cpt.framework.web.JsonResult;
import com.material.c2c.core.UUIDGenerator;
import com.material.c2c.sysconfig.entity.XTPZ_SJZD;
import com.material.c2c.sysconfig.model.DataDictionaryDTO;
import com.material.c2c.sysconfig.service.DataDictionaryService;

/**
 * 数据字典 Controller
 * 
 * @since 1.0
 * @version 1.0
 * @author sunfeng (sun.feng@sccaptain.com.cn)
 */
@Controller
@RequestMapping("/sysconfig/dataDictionary")
public class DataDictionaryController {

  @Autowired
  private DataDictionaryService dataDictionaryService;

  /**
   * 得到所有的字典项
   */
  @RequestMapping(value = "/listDictionaryItem", method = RequestMethod.GET)
  @ResponseBody
  public JsonResult getAllDictionaryItem() {
    List<XTPZ_SJZD> resultList = dataDictionaryService.findAllDataDictionaryItem();
    return JsonResult.success(resultList);
  }

  /**
   * 得到指定字典项下的所有的字典项值
   */
  @RequestMapping(value = "/listDictionaryItemValue/{dictionaryItemId}", method = RequestMethod.GET)
  @ResponseBody
  public JsonResult getDictionaryItemValueList(@PathVariable("dictionaryItemId") String itemId) {
    List<DataDictionaryDTO> resultList = dataDictionaryService.findDictionaryItemValueByItemId(itemId);
    return JsonResult.success(resultList);
  }

  /**
   * 新增数据字典项值信息
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  @ResponseBody
  public JsonResult add(@RequestBody XTPZ_SJZD inputs) {
    inputs.setId(UUIDGenerator.generate().toString());
    if (dataDictionaryService.validateBmExists(inputs.getSjid(), inputs.getBm(), inputs.getId())) {
      return JsonResult.failed("该字典项内已经存在此编码");
    } else {
      int maxBmLength = dataDictionaryService.getMaxBmLengthByDictionaryItemValueList(
          dataDictionaryService.findDictionaryItemValueByItemId(inputs.getSjid()));
      if (maxBmLength != 0 && inputs.getBm().length() > maxBmLength) {
        return JsonResult.failed("该值的编码长度已经超过此字典项内所存在值的编码的最大长度");
      } else {
        // 是否禁用设置为0
        inputs.setSfjy(0);
        // 是否自定义设置为1
        inputs.setSfzdy(Boolean.TRUE);
        // 并将名称同时赋值给自定义名称
        inputs.setZdymc(inputs.getMc());
        // 设置级别为1
        inputs.setJb(1);
        dataDictionaryService.add(inputs);
        return JsonResult.SUCCEED;
      }
    }
  }

  /**
   * 修改数据字典项值信息
   */
  @RequestMapping(value = "/update", method = RequestMethod.POST)
  @ResponseBody
  public JsonResult update(@RequestBody XTPZ_SJZD inputs) {
    // 字典项值的更新只能更新名称
    XTPZ_SJZD sourceEntity = dataDictionaryService.getEntityById(inputs.getId());
    if (null != sourceEntity.getSfzdy()) {
      dataDictionaryService.update(sourceEntity, inputs);
      return JsonResult.SUCCEED;
    } else {
      return JsonResult.failed("该记录的是否自定义属性为空，无法更新");
    }
  }
}
